<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  <meta name="apple-mobile-web-app-capable" content="yes">
  <meta name="apple-mobile-web-app-status-bar-style" content="black">
  <meta name="apple-mobile-web-app-title" content="">
  <meta name="format-detection" content="telephone=no">
  <meta name="full-screen" content="yes">
  <meta name="x5-page-mode" content="app">
  <meta name="x5-fullscreen" content="true">
  <meta name="browsermode" content="application">
  <meta content="" name="keywords">
  <meta content="" name="description">
  <link rel="stylesheet" href="https://cdn.bootcss.com/weui/1.1.3/style/weui.min.css">
  <link rel="stylesheet" href="https://cdn.bootcss.com/jquery-weui/1.2.1/css/jquery-weui.min.css">
  <link rel="stylesheet" href="./js/layer_mobile/need/layer.css">
  <link rel="stylesheet" href="./css/style.css?ver=1.2">
  <title>代理申请</title>
  <style>
    .weui-btn_primary[disabled] { opacity: .7; }
    #pay-speed, #proxybtn, #proinfo { display: none; } 
  </style>
</head>
<body>
  <div class="page__hd">
    <div class="page__title">代理申请</div>
    <div class="page__desc">请 <span class="theme-color">如实</span> 填写您的信息</div>
  </div>
  <!-- 基本信息 -->
  <div class="weui-cells weui-cells_form">
    <div class="weui-cell">
      <div class="weui-cell__bd">
        <input id='name' class="weui-input" type="text" placeholder="请输入姓名">
      </div>
    </div>
    <div class="weui-cell">
      <div class="weui-cell__bd">
        <input id='tel' class="weui-input" type="tel" placeholder="请输入手机号">
      </div>
    </div>
  </div>

  <!-- 服务条款 -->
  <!-- <label for="weuiAgree" class="weui-agree">
    <input id="weuiAgree" type="checkbox" class="weui-agree__checkbox">
    <span class="weui-agree__text">
      阅读并同意<a href="javascript:void(0);">《相关条款》</a>
    </span>
  </label> -->
  <button class="weui-btn weui-btn_primary submit-btn" id="pay-btn">申请成为代理</button>
  <a href="proxySpeed.html" class="weui-btn weui-btn_primary submit-btn" id="pay-speed">查看申请进度</a>
  <a href="proxy.html" class="weui-btn weui-btn_primary submit-btn" id="proxybtn">代理中心</a>
  <!-- 申请驳回 -->
  <div class="m-alert m-alert--error" id='proinfo'>
    <b>申请驳回：</b><span></span>
  </div>

  <script src="https://cdn.bootcss.com/jquery/1.11.0/jquery.min.js"></script>
  <script src="https://cdn.bootcss.com/vConsole/3.3.0/vconsole.min.js"></script>
  <script src="./js/tools2.js?ver=2.1"></script>
  <script src="./js/jweixin-1.4.0.js?ver=1.0"></script>
  <script src="./js/layer_mobile/layer.js"></script>
  <script>
    // var vConsole = new VConsole();
    var mid = tools.getItem('userInfo').user_code;   // 用户id
    var status = 0;   // 为3时，重新申请
    $(function() {
      tools.login(function() {
        getSpeed();
      })
      // 申请成为平台代理
      $('#pay-btn').on('click', function() {
        if(!validate()) return;
        layer.open({ type: 2, shadeClose: false });
        if(status == 3) {
          // 重新申请
          applyAgain();
        } else {
          applyPro();
        }
      })
    })

    // 重新申请代理
    function applyAgain() {
      tools.ajax({
        type: 'PUT',
        url: '/proxy/' + mid,
        data: {
          name: $('#name').val(),
          tel: $('#tel').val()
        },
        success: function(res) {
          if(res.code == 0) {
            $('#pay-speed').show();
            $('#pay-btn').hide();
            window.location.replace('applyMsg.html')
          } else {
            layer.open({ content: res.msg, skin: 'msg', time: 2 });
          }
        },
        complete: function(xhr) {
          layer.closeAll();
        }
      })
    }

    // 申请成为代理
    function applyPro() {
      tools.ajax({
        type: 'POST',
        url: '/proxy',
        data: {
          name: $('#name').val(),
          tel: $('#tel').val(),
          // mid: mid,
        },
        success: function(res) {
          if(res.code == 0) {
            $('#pay-speed').show();
            $('#pay-btn').hide();
            window.location.replace('applyMsg.html')
          } else {
            layer.open({ content: res.msg, skin: 'msg', time: 2 });
          }
        },
        complete: function(xhr) {
          layer.closeAll();
        }
      })
    }

    // 验证
    function validate() {
      var telReg = /^(13[0-9]|14[579]|15[0-3,5-9]|16[6]|17[0135678]|18[0-9]|19[89])\d{8}$/;
      if(!$('#name').val()) {
        layer.open({ content: '请填写姓名', skin: 'msg', time: 2 });
        return false;
      } else if(!$('#tel').val()) {
        layer.open({ content: '请填写手机号', skin: 'msg', time: 2 });
        return false;
      } else if(!telReg.test($('#tel').val())) {
        layer.open({ content: '手机号格式不正确', skin: 'msg', time: 2 });
        return false;
      }
      return true;
    }

    // 获取申请进度
    function getSpeed() {
      tools.ajax({
        url: "/proxy/" + mid,
        success: function(res) {
          if(res.code == 0) {
            var data = res.data;
            status = data.status;
            var $name = $('#name'), $tel = $('#tel');
            $name.val(data.name);
            $tel.val(data.tel);
            $spbtn = $("#pay-speed");     // 查看进度按钮
            $applybtn = $("#pay-btn");    // 申请代理按钮
            $probtn = $('#proxybtn');     // 进入代理中心按钮
            $proinfo = $('#proinfo');     // 驳回信息
            if(data.status == 1) {    // 未处理
              $name.attr({disabled:true});
              $tel.attr({disabled:true});
              $spbtn.css({display:"block"});
              $applybtn.hide();
            } else if(data.status == 2) {   // 通过
              $name.attr({disabled:true});
              $tel.attr({disabled:true});
              $spbtn.hide();
              $applybtn.hide();
              $probtn.css({display:"block"});
            } else if(data.status == 3) {   // 驳回
              $applybtn.html('重新申请');
              $proinfo.show();
              $proinfo.children('span').html(data.info);
            }
          }
        }
      })
    }

    wx.ready(function() {
      var options = { title: "代理申请", desc: "申请成为代理" };
      tools.shareTimeline(options)
      tools.shareAppMessage(options)
    })
  </script>
</body>
</html>